package fragment;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lanou.horo.myapplication.R;

import java.io.ByteArrayOutputStream;

import application.BasisApplication;
import fragment.classicfragments.ClassifDetalsFragment;
import tools.CustomProgressDialog;

/**
 * fragment 基类
 * Created by dllo on 15/11/16.
 */
public abstract class AbsBaseFragment extends Fragment implements View.OnTouchListener {
    private CustomProgressDialog dialog;
    private View view;
    private FragmentManager fm;
    private FragmentTransaction ft;

    /**
     * 获得传入的数据
     *
     * @param savedInstanceState
     */
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dialog = new CustomProgressDialog(getContext(), "", R.anim.loding_better);
        dialog.setCanceledOnTouchOutside(false);
    }

    /**
     * 获得布局
     *
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(getLayoutView(), container, false);
    }

    /**
     * 初始化组件
     *
     * @param view
     * @param savedInstanceState
     */
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        this.view = view;
        this.view.setOnTouchListener(this);
        initView();
    }

    protected View returnView() {
        return this.view;
    }

    protected <T extends View> T findView(int resId) {
        T t = (T) view.findViewById(resId);
        return t;
    }

    /**
     * 初始化数据
     *
     * @param savedInstanceState
     */
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }

    /**
     * 返回值为所要加载的布局文件
     *
     * @return
     */
    protected abstract int getLayoutView();

    /**
     * 初始化控件
     */
    protected abstract void initView();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 显示dialog
     */
    protected void showDialog() {
        dialog.show();
    }

    /**
     * 隐藏dialog
     */
    protected void dismissDialog() {
        dialog.dismiss();
    }

    /**
     * Toast一个数据加载失败
     */
    protected void toastError() {
        showCustomToast("数据加载失败*_*!", R.mipmap.link);
    }

    /**
     * 警告 toastwarning
     */
    protected void toastWarning(String toast) {
        showCustomToast(toast, R.mipmap.device_intro);
    }

    /**
     * 正确的toast
     */
    protected void toastPrompt(String toast) {
        showCustomToast(toast, R.mipmap.wifi_device_selected);
    }

    /**
     * 自定义Toast
     *
     * @param s
     * @param image
     */
    private void showCustomToast(String s, int image) {
        //获取LayoutInflater对象，该对象可以将布局文件转换成与之一致的view对象
        LayoutInflater inflater = getActivity().getLayoutInflater();
        //将布局文件转换成相应的View对象
        View layout = inflater.inflate(R.layout.custome_toast,
                (ViewGroup) inflater.inflate(getLayoutView(), null).findViewById(R.id.toast_layout_root));
        //从layout中按照id查找imageView对象
        ImageView imageView = (ImageView) layout.findViewById(R.id.ivForToast);
        //设置ImageView的图片
        imageView.setBackgroundResource(image);
        //从layout中按照id查找TextView对象
        TextView textView = (TextView) layout.findViewById(R.id.tvForToast);
        //设置TextView的text内容
        textView.setText(s);
        //实例化一个Toast对象
        Toast toast = new Toast(BasisApplication.getContext());
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        toast.setView(layout);
        toast.show();
    }

    /**
     * 点击跳转 替换fragment
     *
     * @param id
     * @param f
     * @return
     */
    protected Fragment replaceFragment(int id, Fragment f) {
        getActivity().getSupportFragmentManager().beginTransaction().
                replace(id, f).commit();
        return f;
    }

    @Override
    public void onPause() {
        super.onPause();
    }



    /**
     * 返回
     */
    public void goBack() {
        if (fm == null) {
            fm = getFragmentManager();
        }
        fm.popBackStack();
        fm.popBackStackImmediate("", 1);
    }


    /**
     * 跳转
     *
     * @param fragment
     */
    public void goToNext(Fragment fragment) {
        if (fm == null) {
            fm = getFragmentManager();
        }
        ft = fm.beginTransaction();
        ft.setCustomAnimations(
                R.anim.push_left_in,
                R.anim.no_push,
                R.anim.no_push,
                R.anim.push_right_out
        );
        ft.add(R.id.layout_replace, fragment);
        ft.addToBackStack(null);
        ft.commitAllowingStateLoss();
    }



    //跳转playfragment
    public void goToPlay(long TrackId) {
        if (fm == null) {
            fm = getFragmentManager();
        }
        ft = fm.beginTransaction();
        ft.setCustomAnimations(
                R.anim.push_shang_in,
                R.anim.no_push,
                R.anim.no_push,
                R.anim.push_shang_out
        );
        ft.add(R.id.replace_all, new PlayFragment(TrackId));
        ft.addToBackStack(null);
        ft.commitAllowingStateLoss();

    }


   public void gotoNextCollect(long albumId){
       if (fm == null){
           fm = getFragmentManager();
       }
       ft = fm.beginTransaction();
       ft.setCustomAnimations(
               R.anim.push_shang_in,
               R.anim.no_push,
               R.anim.no_push,
               R.anim.push_shang_out
       );
       ft.add(R.id.replace_all, new ClassifDetalsFragment(albumId));
       ft.addToBackStack(null);
       ft.commitAllowingStateLoss();
    }




    public void goToMenu(Fragment f) {
        if (fm == null)
            fm = getFragmentManager();
        ft = fm.beginTransaction();
        ft.setCustomAnimations(
                R.anim.push_left_in,
                R.anim.no_push,
                R.anim.no_push,
                R.anim.push_right_out
        );
        ft.add(R.id.replace_all, f);
        ft.addToBackStack(null);
        ft.commitAllowingStateLoss();
    }

    public byte[] BitmapToBytes(Bitmap bm) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }

    public Bitmap BytesToBimap(byte[] b) {
        if (b.length != 0) {
            return BitmapFactory.decodeByteArray(b, 0, b.length);
        } else {
            return null;
        }
    }

    public static Bitmap drawableToBitmap(Drawable drawable) {
        // 取 drawable 的长宽
        int w = drawable.getIntrinsicWidth();
        int h = drawable.getIntrinsicHeight();
        // 取 drawable 的颜色格式
        Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                : Bitmap.Config.RGB_565;
        // 建立对应 bitmap
        Bitmap bitmap = Bitmap.createBitmap(w, h, config);
        // 建立对应 bitmap 的画布
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, w, h);
        // 把 drawable 内容画到画布中
        drawable.draw(canvas);
        return bitmap;
    }

}
